package org.onepiece.admin.common.config.mybatisplus;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.onepiece.admin.controller.BaseController;
import org.onepiece.admin.entity.BaseEntity;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zhujianke@gaodun.com
 * @since 2022/8/21 22:00
 * <p>
 * https://baomidou.com/pages/981406/
 */
public class Generator {

    private static final String PROJECT_PATH = System.getProperty("user.dir");

    /**
     * 数据库配置(DataSourceConfig)
     */
    private static DataSourceConfig.Builder buildDataSourceConfig() {
        return new DataSourceConfig
                .Builder("jdbc:mysql://121.4.152.107:33068/onepiece?serverTimezone=Asia/Shanghai", "root", "123456");
    }

    private static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {

        FastAutoGenerator.create(buildDataSourceConfig())
                .globalConfig(builder -> builder.fileOverride()
                        .outputDir(PROJECT_PATH + "/src/main/java")
                        .author("hautxxxyzjk@163.com")
                        .dateType(DateType.TIME_PACK)
                        .commentDate("yyyy-MM-dd HH:mm:ss")
                        .build())
                .packageConfig(builder -> builder.parent("org.onepiece.admin")
                        .moduleName("sys")
                        .entity("user")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .xml("mapper.xml")
                        .controller("controller")
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, PROJECT_PATH + "/src/main/resources/mapper"))
                        .build())
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))

                        .entityBuilder()
                        .superClass(BaseEntity.class)
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .enableTableFieldAnnotation()
                        .versionColumnName("version")
                        .versionPropertyName("version")
                        .logicDeleteColumnName("deleted_flag")
                        .logicDeletePropertyName("deleted_flag")
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .addSuperEntityColumns("id", "version", "created_by", "created_time", "updated_by", "updated_time", "deleted_by", "deleted_flag", "deleted_time")
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        .idType(IdType.AUTO)
                        .formatFileName("%s")

                        .controllerBuilder()
                        .superClass(BaseController.class)
                        .enableHyphenStyle()
                        .enableRestStyle()
                        .formatFileName("%sController")

                        .serviceBuilder()
                        .superServiceClass(IService.class)
                        .superServiceImplClass(ServiceImpl.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")

                        .mapperBuilder()
                        .superClass(BaseMapper.class)
                        .enableMapperAnnotation()
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapperXml")

                        .build())
                .templateEngine(new VelocityTemplateEngine())
                .execute();

    }
}
